package com.zs.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * Description:
 *
 * @author: zsq-1186
 * Date: 2017-12-04-11:48
 */

@Service
public class RedisService {

    /**
     * 默认过期时间
     */
    private final static long EXPIRE=60*30*1000;

    /***
     * redisTemplate.opsForValue();//操作字符串
     redisTemplate.opsForHash();//操作hash
     redisTemplate.opsForList();//操作list
     redisTemplate.opsForSet();//操作set
     redisTemplate.opsForZSet();//操作有序set
     */
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     *  保存
     * @param key
     * @param value
     */
    public void put(String key,String value){
        redisTemplate.opsForValue().set(key,value);
        redisTemplate.expire(key,EXPIRE, TimeUnit.SECONDS);
    }

    /**
     * 保存
     * @param key
     * @param value
     * @param expire
     */
    public void put(String key,String value,long expire){
        redisTemplate.opsForValue().set(key,value);
        redisTemplate.expire(key,expire, TimeUnit.SECONDS);
    }


    /**
     * 获取
     * @param key
     * @return
     */
    public String get(String key){
        return  redisTemplate.opsForValue().get(key);
    }


    /**
     *获取旧值并设置新值
     * @param key
     * @return
     */
    public String get(String key,String newValue){
        return  redisTemplate.opsForValue().getAndSet(key,newValue);
    }

}
